<?php

namespace app\admin\controller\general;

use app\admin\model\cms\Channels;
use app\common\controller\Backend;
use app\common\model\Channel;
use app\common\model\Config as ConfigModel;

/**
 *
 * 文章快速管理(改的是config表)
 * Class ArticleManagement
 * @package app\admin\controller
 */
class ArticleManagementConfig extends Backend
{

    /**
     * Score模型对象
     * @var \app\admin\model\Score
     */



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
   public function index($ids = null)
   {
       //首页论坛文章快速发帖
       $plate_celerity_post_config_row = ConfigModel::where('name','plate_post_channels_id')->find();
       $categoryList = Channels::where('parent_id',3)->field('id,name')->select();
       $is_auto_approve_plaza = ConfigModel::where('name','is_auto_approve_plaza')->find();
       $is_auto_approve_news = ConfigModel::where('name','is_auto_approve_news')->find();
       $is_auto_approve_plate = ConfigModel::where('name','is_auto_approve_plate')->find();
       //首页广场文章快速发帖
       $plaza_celerity_post_config_row = ConfigModel::where('name','plaza_post_channels_id')->find();
       $plazaCelerityCategoryList = Channels::where('parent_id','>',0)->whereIn('parent_id',[3])->field('id,name')->select();//1=首页广场,3=债市论坛,5=新闻资讯

       //问答社区快速发
       $plate_celerity_post_quora_config_row = ConfigModel::where('name','plate_post_quora_channels_id')->find();


       $this->view->assign("categoryList",$categoryList);
       $this->view->assign("plazaCelerityCategoryList",$plazaCelerityCategoryList);
       $this->view->assign("plaza_celerity_post_config_row",$plaza_celerity_post_config_row);
       $this->view->assign("plate_celerity_post_config_row",$plate_celerity_post_config_row);
       $this->view->assign("plate_celerity_post_quora_config_row",$plate_celerity_post_quora_config_row);
       $this->view->assign("is_auto_approve_plaza",$is_auto_approve_plaza);
       $this->view->assign("is_auto_approve_news",$is_auto_approve_news);
       $this->view->assign("is_auto_approve_plate",$is_auto_approve_plate);

       if ($this->request->isPost() == false) {
           return $this->view->fetch('general/article_management_config/edit');
       } else {
           $row = $this->request->param();

           foreach($row as $k=>$v){
               if (in_array($k,['plate_post_channels_id','plate_post_quora_channels_id','is_auto_approve_plaza','is_auto_approve_news','is_auto_approve_plate','plaza_post_channels_id'])){
                   if ($k == 'plaza_post_channels_id'){
                       $pid = Channel::where('id',$v)->value('parent_id');
                       switch ($pid){
                           case 1:
                               $category_type = 'plaza';//类型|plaza:广场帖子,plate:论坛帖子,news:新闻
                               break;
                           case 3:
                               $category_type = 'plate';//类型|plaza:广场帖子,plate:论坛帖子,news:新闻
                               break;
                           case 5:
                               $category_type = 'news';//类型|plaza:广场帖子,plate:论坛帖子,news:新闻
                               break;
                           default:
                               $this->error('该分类不符合设置');
                       }
                       ConfigModel::where('name','plaza_post_channels_category_type')->update(['value'=>$category_type]);
                   }
                   ConfigModel::where('name',$k)->update(['value'=>$v]);
               }
           }
       }
       $url = $this->request->url();
       return redirect($url);
   }

}
